package com.mooc.yohoshop.ui.detail;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.mooc.yohoshop.App;
import com.mooc.yohoshop.R;
import com.mooc.yohoshop.dao.DaoDBUtils;
import com.mooc.yohoshop.dao.OffLineCarDaoEntity;
import com.mooc.yohoshop.entity.BaseEntity;
import com.mooc.yohoshop.entity.CarEntity;
import com.mooc.yohoshop.entity.DetailEntity;
import com.mooc.yohoshop.entity.GoodsEntity;
import com.mooc.yohoshop.entity.LoadGoodsEntity;
import com.mooc.yohoshop.entity.OffLineCarEntity;
import com.mooc.yohoshop.entity.SuccessEntity;
import com.mooc.yohoshop.filed.ApiConst;
import com.mooc.yohoshop.ui.base.BaseActivity;
import com.mooc.yohoshop.ui.car.CarActivity;
import com.mooc.yohoshop.ui.home.HomeGoodsAdapter;
import com.mooc.yohoshop.widget.HomeGoodsDivider;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

public class GoodsDetailActivity extends BaseActivity<GoodsDetailRepo> implements
        OnRefreshLoadMoreListener, View.OnClickListener, OnItemClickListener {

    private long goodsId = 0;
    private long typeId = 0;
    private int page = 1;
    private DetailEntity detailEntity;
    private View valuesHead,goodsHead;
    private RecyclerView goods_values_rv,values_rv;
    private ViewPager goods_values_vp;
    private TextView goods_values_name_tv,goods_values_price_tv;
    private DetailBannerAdapter vpAdapter;
    private DetailImgAdapter imgAdapter = new DetailImgAdapter();
    private HomeGoodsAdapter adapter = new HomeGoodsAdapter();

    @Override
    protected int layout() {
        return R.layout.activity_goods_values;
    }

    @Override
    protected void init() {
        f(R.id.goods_values_back_img).setOnClickListener(this::onClick);
        f(R.id.seckill_values_share_tv).setOnClickListener(this::onClick);
        f(R.id.seckill_values_car_tv).setOnClickListener(this::onClick);
        f(R.id.seckill_values_add_car).setOnClickListener(this::onClick);
        valuesHead = View.inflate(this,R.layout.view_goods_values_head,null);
        goodsHead = View.inflate(this,R.layout.view_goods_values,null);
        goods_values_rv = f(R.id.goods_values_rv);
        values_rv = valuesHead.findViewById(R.id.values_rv);
        goods_values_vp = goodsHead.findViewById(R.id.goods_values_vp);
        goods_values_name_tv = goodsHead.findViewById(R.id.goods_values_name_tv);
        goods_values_price_tv = goodsHead.findViewById(R.id.goods_values_price_tv);
        adapter.addHeaderView(goodsHead);
        adapter.addHeaderView(valuesHead);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        values_rv.setLayoutManager(linearLayoutManager);
        values_rv.setAdapter(imgAdapter);
        GridLayoutManager manager = new GridLayoutManager(this,2);
        manager.setOrientation(GridLayoutManager.VERTICAL);
        goods_values_rv.addItemDecoration(new HomeGoodsDivider(this));
        goods_values_rv.setLayoutManager(manager);
        goods_values_rv.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        goodsId = getIntent().getLongExtra("goodsId",0);
        typeId = getIntent().getLongExtra("typeId",0);
        ArrayList<HashMap<String,Object>> list=new ArrayList<>();
        HashMap<String,Object> detailMap = new HashMap<>();
        detailMap.put("goodsId",goodsId);
        list.add(detailMap);
        HashMap<String,Object> goodsMap = new HashMap<>();
        goodsMap.put("page",page);
        goodsMap.put("typeId",typeId);
        list.add(goodsMap);
        request(repo.requestDetails(list));
    }

    @Override
    protected void result(BaseEntity baseEntity) {
        if (baseEntity instanceof DetailEntity){
            detailEntity = (DetailEntity) baseEntity;
            vpAdapter = new DetailBannerAdapter(detailEntity.data.imgs);
            goods_values_vp.setAdapter(vpAdapter);
            goods_values_name_tv.setText(detailEntity.data.values.goods_name);
            goods_values_price_tv.setText(detailEntity.data.values.goods_discount_price);
            imgAdapter.setNewInstance(detailEntity.data.imgs);
        }else if (baseEntity instanceof GoodsEntity){
            GoodsEntity goodsEntity = (GoodsEntity) baseEntity;
            adapter.setNewInstance(goodsEntity.data);
        }else if (baseEntity instanceof LoadGoodsEntity){
            LoadGoodsEntity loadGoodsEntity = (LoadGoodsEntity) baseEntity;
            adapter.addData(loadGoodsEntity.data);
        }else if (baseEntity instanceof SuccessEntity){
            if (baseEntity.message.equals("添加成功")){
                ToastUtils.showShort("购物车添加成功");
            }
        }
    }

    @Override
    public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
        refreshLayout.finishLoadMore();
        HashMap<String,Object> goodsMap = new HashMap<>();
        goodsMap.put("page",page);
        goodsMap.put("typeId",typeId);
        request(repo.requestGoods(goodsMap));
    }

    @Override
    public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
        refreshLayout.finishRefresh();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.goods_values_back_img){
            finish();
        }else  if (R.id.seckill_values_share_tv == v.getId()){
        }else  if (R.id.seckill_values_car_tv == v.getId()){
            startActivity(new Intent(this, CarActivity.class));
        }else if (R.id.seckill_values_add_car == v.getId()){
            if (App.getInstance().isLogin()){
                HashMap<String,Object>map = new HashMap<>();
                ArrayList<CarEntity.CarGoodsValues>list = new ArrayList();
                CarEntity.CarGoodsValues values = new CarEntity.CarGoodsValues();
                values.car_goods_id = detailEntity.data.values.goods_id;
                values.car_id = 0;
                values.car_path = detailEntity.data.values.goods_img;
                values.goods_id = detailEntity.data.values.goods_id;
                values.shop_name = detailEntity.data.values.goods_name;
                values.shop_color = "红";
                values.shop_num = "1";
                values.shop_size = "XL";
                values.shop_price = detailEntity.data.values.goods_discount_price;
                values.shop_select = "0";
                list.add(values);
                map.put("car_goods",list);
                map.put("user_id",App.getInstance().getUserId());
                request(repo.requestAddCar(map));
            }else {
                if (detailEntity == null)
                    return;
                OffLineCarEntity offEntity = new OffLineCarEntity();
                offEntity.goods_id = detailEntity.data.values.goods_id;
                offEntity.shop_name = detailEntity.data.values.goods_name;
                offEntity.shop_num = ""+1;
                offEntity.shop_color = "红";
                offEntity.shop_price = ""+detailEntity.data.values.goods_discount_price;
                offEntity.shop_size = "XL";
                offEntity.car_path = ApiConst.IMGURL+detailEntity.data.values.goods_img;
                offEntity.shop_select = "1";
                new OffLineCarDaoEntity().insert(offEntity);
//                DaoDBUtils.getInstance().getDaoSession().getOffLineCarEntityDao()
//                        .insert(offEntity);
            }
        }
    }

    @Override
    public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
        Intent intent = new Intent(this,GoodsDetailActivity.class);
        intent.putExtra("goodsId",this.adapter.getItem(position).goods_id);
        intent.putExtra("typeId",typeId);
        startActivity(intent);
    }
}
